package com.tjvib.view.activity;

import static com.tjvib.Constants.SUCCESS;
import static com.tjvib.util.FormatUtil.isValidPhoneNumber;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.tjvib.R;
import com.tjvib.base.BaseActivity;
import com.tjvib.presenter.BasePresenter;
import com.tjvib.util.LogUtil;
import com.tjvib.util.Manager.AppManager;
import com.tjvib.util.Manager.UserManager;
import com.tjvib.util.PermissionUtil;
import com.tjvib.util.PressUtil;
import com.tjvib.util.ToastUtil;
import com.tjvib.util.retrofit.ApiInterface;
import com.tjvib.util.retrofit.BaseResponse;
import com.tjvib.util.retrofit.RetrofitHelper;
import com.tjvib.widget.AndroidScheduler;
import com.tjvib.widget.ItemGroup;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SafeActivity extends BaseActivity {

    private ItemGroup safe_ig_phone, safe_ig_device, safe_ig_version, safe_ig_pwd, safe_ig_model;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_safe;
    }

    @Override
    protected boolean initView() {
        safe_ig_phone = findViewById(R.id.safe_ig_phone);
        safe_ig_device = findViewById(R.id.safe_ig_device);
        safe_ig_model = findViewById(R.id.safe_ig_model);
        safe_ig_version = findViewById(R.id.safe_ig_version);
        safe_ig_pwd = findViewById(R.id.safe_ig_pwd);
        safe_ig_pwd.setOnClickListener(v -> {
            showDialog(this);
        });

        PressUtil.setPressChange(SafeActivity.this, safe_ig_pwd);
        return true;
    }

    @Override
    protected boolean initData() {
        safe_ig_phone.setItem_group_et_con(UserManager.getInstance().getPhoneNumber());
        safe_ig_device.setItem_group_et_con(android.os.Build.DEVICE);
        safe_ig_model.setItem_group_et_con(android.os.Build.MODEL);
        safe_ig_version.setItem_group_et_con(android.os.Build.VERSION.RELEASE);
        return true;
    }

    @Override
    protected BasePresenter genPresenter() {
        return null;
    }


    public void showDialog(Context context) {
        Dialog dialog = new Dialog(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_change_pwd, null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();

        Button dialog_pwd_btn_sms = view.findViewById(R.id.dialog_pwd_btn_sms);
        EditText dialog_pwd_et_sms = view.findViewById(R.id.dialog_pwd_et_sms);
        EditText dialog_pwd_et_psw = view.findViewById(R.id.dialog_pwd_et_psw);

        // 倒计时
        class SmsCountDownTimer extends CountDownTimer {
            public SmsCountDownTimer(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
            }

            @Override
            public void onTick(long l) {
                dialog_pwd_btn_sms.setEnabled(false);
                dialog_pwd_btn_sms.setClickable(false);
                dialog_pwd_btn_sms.setText(l / 1000 + "秒");
            }

            @Override
            public void onFinish() {
                dialog_pwd_btn_sms.setText("重新获取");
                dialog_pwd_btn_sms.setEnabled(true);
                dialog_pwd_btn_sms.setClickable(true);
            }
        }

        SmsCountDownTimer timer = new SmsCountDownTimer(60000, 1000);

        dialog_pwd_btn_sms.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                String phone = UserManager.getInstance().getPhoneNumber();
                if (phone.equals("") || phone == null) {
                    ToastUtil.show("请填写手机号");
                } else if (!isValidPhoneNumber(phone)) {
                    ToastUtil.show("手机号格式不正确");
                } else {
                    RetrofitHelper.getInstance().create().get_sms_code(phone)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidScheduler.mainThread())
                            .subscribe(new Observer<BaseResponse>() {
                                @Override
                                public void onSubscribe(Disposable d) {
                                }

                                @Override
                                public void onNext(BaseResponse baseResponse) {
                                    if (baseResponse.getCode() == SUCCESS) {
                                        ToastUtil.show("发送成功");
                                        timer.start();
                                    } else
                                        ToastUtil.show("" + baseResponse.getMessage());
                                    LogUtil.i("dialog_pwd_btn_sms " + baseResponse.getCode() + " " + baseResponse.getMessage()
                                            + " " + baseResponse.getData());
                                }

                                @Override
                                public void onError(Throwable e) {
                                    LogUtil.e(e.getMessage());
                                    ToastUtil.show(e.getMessage());
                                }

                                @Override
                                public void onComplete() {
                                }
                            });
                }
            }
        });

        view.findViewById(R.id.dialog_pwd_btn_cancel).setOnClickListener(v -> {
            dialog.dismiss();
        });

        view.findViewById(R.id.dialog_pwd_btn_confirm).setOnClickListener(v -> {
            String code = dialog_pwd_et_sms.getText().toString().trim();
            String psw = dialog_pwd_et_psw.getText().toString().trim();
            RetrofitHelper.getInstance().create()
                    .change_passwd("" + UserManager.getInstance().getUserId(), code, psw)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidScheduler.mainThread())
                    .subscribe(new Observer<BaseResponse>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                        }

                        @Override
                        public void onNext(BaseResponse baseResponse) {
                            if (baseResponse.getCode() == SUCCESS) {
                                ToastUtil.show("修改成功，请重新登陆");
                                dialog.dismiss();
                                startActivity(new Intent(getApplicationContext(), BootingActivity.class));
                                AppManager.getInstance().finishOtherActivity(BootingActivity.class);
                            } else {
                                ToastUtil.show("修改失败，请稍后重试");
                                LogUtil.e(baseResponse.getCode()+" "+baseResponse.getMessage()+" "+baseResponse.getData());
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtil.show(e.getMessage());
                            LogUtil.e(e.getMessage());
                        }

                        @Override
                        public void onComplete() {
                        }
                    });
            dialog.dismiss();
        });
    }
}